package org.fuys.coder.domain.act.activity.service.impl;

import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.domain.act.activity.model.vo.CommonActSupportVO;
import org.fuys.coder.domain.act.activity.model.vo.CommonActVO;
import org.fuys.coder.domain.act.activity.repository.ICommonActRepository;
import org.fuys.coder.domain.act.activity.service.ICommonActService;
import org.fuys.coder.domain.act.support.model.vo.ActTypeVO;
import org.fuys.coder.domain.act.support.model.vo.ActVO;
import org.fuys.coder.common.entity.PageDTO;
import org.fuys.coder.domain.file.service.AliYunOSSService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.act.activity.service.impl
 * @className: CommonActServiceImpl
 * @author: WangJing
 * @description: 普通活动服务实现类
 * @date: 2024/8/25 22:41
 * @version: 1.0
 */
@Service
public class CommonActServiceImpl implements ICommonActService {
    @Resource
    private ICommonActRepository commonActRepository;

    @Resource
    private AliYunOSSService aliYunOSSService;

    @Override
    public CommonActVO getActById(Long actId,Integer state) {
        final CommonActVO byId = commonActRepository.getById(actId, state);
        if(byId==null){
            return null;
        }
        byId.setCoverUrl(aliYunOSSService.buildUrl(byId.getCoverId()));
        final CommonActSupportVO actSupport = this.getActSupport(actId);
        byId.setSupportVO(actSupport);
        return byId;
    }

    @Override
    public CommonActSupportVO getActSupport(Long actId) {
        return commonActRepository.getSupportById(actId);
    }

    @Override
    public List<ActVO> getCurHotAct(int num) {
        final List<CommonActVO> curAct = commonActRepository.getCurAct(num);
        if(ObjectUtils.isEmpty(curAct)){
            return null;
        }
        return curAct.stream().map(item -> {
            item.setCoverUrl(aliYunOSSService.buildUrl(item.getCoverId()));
            ActVO actVO = new ActVO();
            actVO.setActData(item);
            return actVO;
        }).collect(Collectors.toList());

    }

    @Override
    public Pair<List<ActVO>,Long> getPage(PageDTO pageDTO) {
        final Pair<List<CommonActVO>,Long> page = commonActRepository.getPage(pageDTO);
        if(ObjectUtils.isEmpty(page)){
            return null;
        }
        final List<ActVO> collect = page.getLeft().stream().map(item -> {
            item.setCoverUrl(aliYunOSSService.buildUrl(item.getCoverId()));
            ActVO actVO = new ActVO();
            actVO.setActData(item);
            actVO.setType(ActTypeVO.COMMON);
            return actVO;
        }).collect(Collectors.toList());
        return new Pair<>(collect,page.getRight());
    }
}
